package com.group.yztcedu.playlive.main.me.View;

import android.content.Context;
import android.os.Bundle;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.alipay.sdk.app.PayTask;
import com.group.yztcedu.playlive.R;
import com.group.yztcedu.playlive.config.PayUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class MeRechargeActivity extends AppCompatActivity {
    private Context mContext = this;
    private List<String> list;
    @BindView(R.id.me_recharge_back)
    ImageView meRechargeBack;
    @BindView(R.id.me_recharge_detil)
    TextView meRechargeDetil;
    @BindView(R.id.me_recharge_group)
    GridView meRechargeGroup;
    @BindView(R.id.me_recharge_prize)
    TextView meRechargePrize;
    @BindView(R.id.me_recharge_recharge)
    Button meRechargeRecharge;

    private int[] prize;
    private int prizeitem = 0;
    private String result;

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case PayUtils.SDK_PAY_FLAG:
                    result = (String) msg.obj;
                    break;
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_me_recharge);
        ButterKnife.bind(this);
        prize = new int[]{2,10,20,50,100,200,500,1000};
        SetText();
        MyAdapter adapter = new MyAdapter(list);
        meRechargeGroup.setAdapter(adapter);

    }

    @OnClick({R.id.me_recharge_back, R.id.me_recharge_recharge})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.me_recharge_back:
                finish();
                break;
            case R.id.me_recharge_recharge:
                PayUtils payUtils = new PayUtils(prizeitem+"");
                payUtils.pay(handler,MeRechargeActivity.this);
                Toast.makeText(MeRechargeActivity.this,result,Toast.LENGTH_SHORT).show();
                break;
        }
    }
    //检查版本的方法
    public void checkVersion(View view) {
        PayTask task = new PayTask(MeRechargeActivity.this);
        String version = task.getVersion();
        Toast.makeText(MeRechargeActivity.this, "当前版本=" + version, Toast.LENGTH_SHORT).show();
    }
    public void SetText(){
        list = new ArrayList<>();
        for(int i=0;i<8;i++){
            list.add(getData(i));
        }
    }
    public String getData(int i){
        String str = "";
        switch (i){
            case 0:
                str = 2000+"玩耍币";
                break;
            case 1:
                str = 10000+"玩耍币";
                break;
            case 2:
                str = 20000+"玩耍币";
                break;
            case 3:
                str = 50000+"玩耍币";
                break;
            case 4:
                str = 100000+"玩耍币";
                break;
            case 5:
                str = 200000+"玩耍币";
                break;
            case 6:
                str = 500000+"玩耍币";
                break;
            case 7:
                str = 1000000+"玩耍币";
                break;
        }
        return str;
    }
    private class MyAdapter extends BaseAdapter {
        private List<String> list;
        private LayoutInflater mInflater;

        public MyAdapter(List<String> list){
            this.list = list;
            mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        }
        @Override
        public int getCount() {
            return list!=null?list.size():0;
        }

        @Override
        public Object getItem(int i) {
            return list!=null?list.get(i):null;
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(final int i, View view, ViewGroup viewGroup) {
            ViewHolder viewHolder;
            if(view == null) {
                view = mInflater.inflate(R.layout.me_recharge_list,viewGroup,false);
                viewHolder = new ViewHolder(view);
                view.setTag(viewHolder);
            }
            viewHolder= (ViewHolder) view.getTag();
            viewHolder.item.setText(list.get(i));
            viewHolder.item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    prizeitem = prize[i];
                    meRechargePrize.setText("售价"+prizeitem+".00元");
                }
            });
            return view;
        }
        public class ViewHolder{
            private Button item;
            ViewHolder(View view){
                item = (Button) view.findViewById(R.id.btn_recharge_item);
            }
        }
    }
}
